package cn.edkuir.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.edkuir.product.entity.ProductAttrValueEntity;
import cn.edkuir.product.service.ProductAttrValueService;
import cn.edkuir.product.vo.AttrRespVo;
import cn.edkuir.product.vo.AttrVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.edkuir.product.entity.AttrEntity;
import cn.edkuir.product.service.AttrService;
import cn.edkuir.common.utils.PageUtils;
import cn.edkuir.common.utils.R;



/**
 * 商品属性
 *
 * @author xinzi
 * @email 1228604898@qq.com
 * @date 2021-04-14 19:48:30
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@PathVariable("catelogId") Long catelogId,
                          @RequestParam Map<String, Object> params,
                          @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryAttrPage(params, catelogId, type);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    //获取spu规格
    @GetMapping("/base/listforspu/{spuId}")
    public R getSpuBaseAttr(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> data = productAttrValueService.getSpuBaseAttr(spuId);

        return R.ok().put("data", data);
    }


    //获取spu规格
    @PostMapping("/update/{spuId}")
    public R updateSpuBaseAttr(@PathVariable("spuId") Long spuId,
                               @RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {
        productAttrValueService.updateSpuBaseAttr(spuId, productAttrValueEntities);

        return R.ok();
    }

}
